<!DOCTYPE html>
<html>
<head>
  <meta charset=utf-8 />
  <title>IsInputPending: clicks should be reported during main thread contention</title>
  </title>
  <div>
    Click the button twice and expect two green lines of text.
    This test should be run under --enable-blink-features=ExperimentalIsInputPending
  </div>
</head>
<button onclick="next()">Start</button>
<p id="preClickResult"></p>
<p id="postClickResult"></p>
<script>

function hasPendingClick() {
  return navigator.scheduling.isInputPending(['click']);
}

// |expected| is the expected value from isInputPending(['click']).
function createResult(actual, expected) {
  const result = document.createElement('div');
  if (actual == expected) {
    result.innerHTML = 'PASSED: isInputPending was ' + actual;
    result.style = 'color:green';
  }
  else {
    result.innerHTML = 'FAILED: isInputPending was ' + actual;
    result.style = 'color:red';
  }
  return result;
}

let state = "start";

function next() {
  switch (state) {
    case "start":
      // Don't return from the event loop until a click is detected.
      while (!hasPendingClick()) {}

      const preClickResult = document.querySelector("#preClickResult");
      preClickResult.appendChild(createResult(true, true));

      state = "clicked";
      break;
    case "clicked":
      // In the click event handler, expect no pending clicks.
      const postClickResult = document.querySelector("#postClickResult");
      postClickResult.appendChild(createResult(hasPendingClick(), false));

      state = "end";
      break;
    case "end":
      break;
  }
}

</script>
</html>
